package com.littlebee.storysuper.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.LogUtils;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.ToastUtil;
import com.littlebee.storysuper.utils.Utils;
import com.littlebee.storysuper.zxing.CaptureActivity;
import com.zss.library.PermissionCallBack;
import com.zss.library.activity.BaseActivity;
import com.zss.library.toolbar.CommonToolbar;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/***
 * 绑定设备
 */
public class BindActivity extends BaseActivity {
    EditText et_username;
    Button btn_submit;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_bind;
    }

    @Override
    public void initView() {
        super.initView();
        et_username = (EditText) findViewById(R.id.et_username);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_username.getText().toString().trim())) {
                    ToastUtil.displayShortToast(R.string.please_driver_txt);

                    return;
                }
                loadData(et_username.getText().toString().trim());
            }
        });
    }

    /**
     * 绑定设备
     */
    public void loadData(String device) {
        Map<String, String> hashParams = new HashMap<>();
        User user = Utils.getUserEntry();
        hashParams.put("phoneId", user.getMobile());
        hashParams.put("deviceid", device);
        // TODO: 2017/8/14
        NetWorkUtils.post(getActivity(), "binddevice", hashParams,true, new NetWorkUtils.IListener() {

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                LogUtils.i("XHX", "XHXSUCC:" + result);
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX", "XHXERROR:" + result);


            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        setStatusBarColor(R.color.module_video_bg);
        CommonToolbar toolbar = getToolbar();
        toolbar.setBgRes(R.color.module_video_bg);
        toolbar.setTitle(getString(R.string.bind_drive_txt));
//        toolbar.setRightText("扫描");
//        toolbar.setOnRightListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                verifyPermissions(getActivity(), Manifest.permission.CAMERA,
//                        new String[]{Manifest.permission.CAMERA}
//                        , 0x01, new PermissionCallBack() {
//                            @Override
//                            public void onGranted() {
//                                if (Utils.isLogin()) {
//                                    Intent intent = new Intent(BindActivity.this, BindActivity.class);
//                                    startActivityForResult(intent, 0x01);
//                                }
//                            }
//
//                            @Override
//                            public void onDenied() {
//                            }
//                        });
//            }
//        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x01 && resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("result");
            if (TextUtils.isEmpty(result)) {
                ToastUtil.displayShortToast(R.string.please_driver_valid_txt);

                return;
            }
            loadData(result);

        }
    }
}
